// create by dplord at 2017-02-06 16:07

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <map>
#include <unordered_map>

#include "unistd.h"

using namespace std;



/**
 * 10个进程, 每个进程创建1000个文件
 * @param filename
 */
void createOneFile(string filename) {
    string s = "";
    string line;
    for (int i = 0; i < 10000; ++i) {
        line = "";
        for (int j = 0; j < 5; ++j) {
            int num = rand() % 10000;
            line += to_string(num);
            line += "\t";
        }
        line += "\n";
        s += line;
    }

    ofstream out(filename, ios::app);
    out << s;
    out.close();
}

int main(int argc, char **arrv) {
    pid_t pid;
    int num = 0;
    int process_num = 10;
    for (int i = 0; i < process_num; ++i) {
        pid = fork();
        //如果子进程不加break, 那么就会循环创建进程,系统进程数满了
        if(pid == 0 || -1 == pid) {
            break;
        } else {
            num += 1;
        }
    }

    string base_path = "/Users/dengpan/tmp/out/r_";
    int n;
    for (int j = 0; j < 1000; ++j) {
        n = j + 1000 * num;
        string fname = base_path + to_string(n);
        createOneFile(fname);
    }

    return 0;
}

